#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswstatstop command
#
#   Syntax: lsswstatstop (-f <frame> -c <cage> | -a) | --help ]
#
#            --help               - Prints this message
#
####################################################################
#
#   Initial version - 02/04/04
#
####################################################################

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

sub validate_props {

	if ($frame_parm =~ /\D+/) {
		# non-digit in frame field
		print "non-digit in frame field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($cage_parm =~ /\D+/) {
		# non-digit in cage field
		print "non-digit in cage field\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

}

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswstatstop";
$BIN    = "/opt/hsc/bin"; 
$PARMS = 0;
$f_USED = 0;
$c_USED = 0;
$a_USED = 0;
#default params
$frame_parm = 0;
$cage_parm = 0;
		
$USAGE = "Usage: lsswstatstop (-f <frame> -c <cage> | -a) | --help ]\n";

# First check for existence of parameters
if ($#ARGV ne -1) {
	$PARMS = 1;
}

$i=0;
# Determine if -f was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-f\S*$/ ) { 
		$f_USED = 1;
        $frame_parm = $ARGV[$i + 1];		
	}
	$i++;
	
}

$i=0;
# Determine if -c was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-c\S*$/ ) { 
		$c_USED = 1;
        $cage_parm = $ARGV[$i + 1];		
	}
	$i++;
}

# Determine if -a was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-a\S*$/ ) { 
		$a_USED = 1;
	}
}

# If there are parms, need to validate and parse
if ($PARMS == 1) {
	# Check for --help (usage)
	foreach $ARGV (@ARGV) {
		if ($ARGV =~ /^\s*--help\S*$/ ) { 
			# Help was specified
			print $USAGE;    #USAGE
			if ($#ARGV == 0) {
				$ret = 0;
			}
			else {
				# If help and anything else was specified,
				#      it's an error
				$ret = 1;
			}
			&exit_routine;
		} #end if
	} #end foreach

    if (($f_USED == 1) && ($c_USED == 1) && ($a_USED == 1)) {
		# Specified -f and -c and -a
		print "-f, -c and -a cannot all be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($c_USED != 1) && ($a_USED != 1)) {
		#No valid flags used
		print "Either -f and -c or -a must be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED == 1) && ($c_USED != 1)) {
		# Specified -f, but not -c
		print "-f used, -c must also be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($c_USED == 1)) {
		# Specified -c, but not -f
		print "-c used, -f must also be used\n\n";
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

	if ($f_USED == 1 || $a_USED == 1) {
		&validate_props;
	}

  	if (($f_USED == 1) && ($c_USED == 1)) {
		$command = "i_stub_FS switchstats -C stopswitch -f ";

		$command = $command . $frame_parm . " -c " . $cage_parm . " -p 0 -y 0 -a 0 -o 0 -v 0 -m switchStats -z 0 -x 0";

		chop($RC = `$BIN/$command`);
    }
	elsif ($a_USED == 1) {
		$command = "i_stub_FS switchstats -C stopall" . " -f 0 -c 0 -p 0 -y 0 -a 0 -o 0 -v 0 -m switchStats -z 0 -x 0";

		chop($RC = `$BIN/$command`);
		
	}
	else {
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

    if ($RC==0)
    {
            print "Command successful.\n";
    }       
    if ($RC==50)
		{
			print "Cannot connect to FNM Daemon\n";
		}
		if ($RC==51)
		{
			print "One or more frame,cage parameters are invalid\n";
		}
		if ($RC==52)
		{
			print "Device Database not available\n";
		}
		if ($RC==70)
		{
			print "Unsupported Switch Statistics command.\n";
		}
		if ($RC==71)
		{
			print "Invalid frame and cage specified.\n";
		}
		if ($RC==72)
		{
			print "Counter already running on specified frame and cage.\n";
		}
		if ($RC==73)
		{
			print "Warning: Counter already running for one or more switches.\n";
            print "Command successful.\n";
		}
		if ($RC==74)
		{
			print "Counter for specified frame and cage is not running.\n";
		}
		if ($RC==75)
		{
			print "No switch counters running.\n";
		}
		if ($RC==76)
		{
			print "Thread failed to initialize.\n";
		}
		if ($RC==77)
		{
			print "Counter not running for one or more switches.\n";
            print "Command successful.\n";			
		}		
        if ($RC==78)
        {
       	    print "Error opening directory specified.  Check to see if directory exists.\n";
        }	

$ret = 0;
$exit_routine;
}
else {
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}
